home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include "global.h"
-
- #include "HFS.h"
- #include <time.h>
- #include <types.h>
- #include "mac_stat.h"
- #include "mac_files.h"
-
- extern errno;
- struct RemoveIt Head;
-
- char *CtoPstr(),*PtoCstr();
-
- /*
- * dir: Create a directory listing in a temp file and return the resulting file
- * descriptor. If full == 1, give a full listing in the temp file; if full
- * == 2, give just a list of names in the temp file; if full ==3, output to
- * console with the full listing. full == 0 is same as full == 2.
- */
- FILE *
- dir(path,full)
- char *path;
- int full;
- {
- WDPBRec MyDisk;
- CInfoPBRec Everything;
- FILE *fp, *fp1;
- OSErr e;
- char *GetPathname();
-
- char *ptr;
- char buff[256];
- char holding_file[256];
- char working_volume[256];
- char dirname[256];
-
- errno = 0;
-
- /* open up DIR.TEMP file for the cases we need it, otherwise direct to stdout */
-
- if ( full < 3) {
- if ( ( fp = fopen(dirbm, "w")) == NULL) {
- printf("Open failed, errno = %d\n",errno);
- return((FILE *)NULL);
- }
- }
- else
- fp = stdout;
-
- /* start process of getting directory */
-
- MyDisk.ioWDProcID = 0L;
- MyDisk.ioWDDirID = 0L;
- MyDisk.ioVRefNum = 0;
- MyDisk.ioWDVRefNum = 0;
- MyDisk.ioNamePtr = (StringPtr)working_volume;
- if (path[0] == NULL)
- path = ":";
- MoveIt(MyDisk.ioNamePtr, path);
-
- if ( (e = PBOpenWD( &MyDisk, FALSE)) != noErr ) {
-
- /*
- * got an error on initial PBOpenWD
- * see if we were passed a file name instead of directory by trying to
- * open it through the library fopen call
- */
- if ( ( fp1 = fopen(path, "r")) == NULL) {
- if (full < 3) {
- fclose(fp);
- unlink(dirbm);
- }
- else
- printf("Can't get directory of '%s'.\n",path);
- return(NULL);
- }
- else {
- fclose(fp1); /* close the fopen file */
-
- /* we were able to open the path as a file.
- * For just the list of file names, strip the latter part
- * of the path and place that in the file. */
-
- if ( (full == 0) || (full == 2) ) {
- ptr = rindex(path, ':');
- if (ptr != NULL) /* found a :, return stuff to right */
- ptr++;
- else
- ptr = path; /* no :, return entire path */
- fprintf(fp,"%s\n", ptr);
- fclose(fp); /* close file, reopen as read only */
- fp = fopen("dir.temp", "r");
- return(fp);
- }
- else {
- /* we have a single file, and we must return full info. Get it. */
- sprintf(holding_file, "%s", path);
- CtoPstr(holding_file);
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
- Everything.hFileInfo.ioFDirIndex = 0;
- Everything.hFileInfo.ioDirID = 0L;
- Everything.hFileInfo.ioCompletion = 0;
-
- if ( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
- ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
- ptr +=3;
- ptr[strlen(ptr)-1] = '\0';
- ptr[strlen(ptr)-8] = '\0';
- fprintf(fp,"%c %8ld %8ld-rf %s %s %s\n",
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?
- 0:Everything.hFileInfo.ioFlLgLen,
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?
- 0:Everything.hFileInfo.ioFlRLgLen, ptr, ptr+19,
- PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
- }
- else
- printf("Error getting directory: %d\n", e);
- }
- if ( full < 3 ) {
- fclose(fp);
- fp = fopen(dirbm, "r");
- }
- return(fp);
- }
- }
-
- /* original PBOpenWD worked */
-
- if (full == 3)
- fprintf(fp,"Directory of %s\n",GetPathname(dirname,MyDisk.ioVRefNum));
-
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
- sprintf(Everything.hFileInfo.ioNamePtr, "\p");
- Everything.hFileInfo.ioFDirIndex = 1;
- Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
- Everything.hFileInfo.ioCompletion = 0;
-
- while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
- if ( e == noErr ) {
- if( full == 0) {
- fprintf(fp, "%s\n",
- PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
- }
- else {
- ptr = ctime((long *)&Everything.hFileInfo.ioFlCrDat);
- ptr +=3;
- ptr[strlen(ptr)-1] = '\0';
- ptr[strlen(ptr)-8] = '\0';
- fprintf(fp,"%c %8ld %8ld-rf %s %s %s\n",
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?'d':'-',
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?
- 0:Everything.hFileInfo.ioFlLgLen,
- (Everything.hFileInfo.ioFlAttrib & (1<<4))?
- 0:Everything.hFileInfo.ioFlRLgLen, ptr, ptr+19,
- PtoCstr((char *) Everything.hFileInfo.ioNamePtr));
- }
-
- Everything.hFileInfo.ioFlLgLen = 0;
- Everything.hFileInfo.ioFDirIndex++;
-
- }
- else
- break; /* go an error other than fnfErr: break */
-
- Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
- }
-
- /*
- * either the PBGetCatInfo finally encountered a fnfErr, or some other error
- * occurred. If we were writing a file, close it then reopen it before returning
- * the file pointer to our caller.
- */
- PBCloseWD(&MyDisk, FALSE);
- if ( full < 3) {
- fclose(fp);
- fp = fopen(dirbm, "r");
- }
- return(fp);
- }
-
- /*
- * MoveIt: this is like a bcopy, but uses pascal string type
- * of data.
- */
-
- MoveIt(to,from)
- char *to, *from;
- {
- int size;
-
- memset(to, 0, 3);
- to[0] = size = strlen(from);
- if ( size <= 0)
- {
- printf("MoveIt: size <= 0 (%d\n", size);
- return;
- }
- to++;
- while( size-- )
- *to++ = *from++;
- }
-
- /*
- * tmpfile: create a temporary file. Remember it so we can delete it later because
- * the mac does not allow the file to be deleted when it is open.
- */
-
- FILE *
- tmpfile()
- {
- FILE *tmp;
- char *mktemp();
- char *ptr;
- char tmpname[16];
-
- strcpy(tmpname,"MLMxxxxxxxx");
- if ( ( ptr = malloc(strlen(temppath)+strlen(tmpname)+1)) == NULLCHAR) {
- printf("tmpfile: can't allocate memory.\n");
- return(NULL);
- }
- (void) mktemp(tmpname); /* replace the xxxxxxxx with something else */
- sprintf(ptr,"%s%s",temppath,tmpname);
- if ( ( tmp = fopen(ptr, "w+") ) == NULL) {
- printf("tmpfile: could not create temp file (%s)\n", ptr);
- (void)free(ptr); /* free up our original name storage */
- return(NULL);
- }
-
- /* save away the name of the file so we can delete it when exiting (ignore
- * errors, if we can't delete at exit it won't harm anything). */
-
- NLadd_name(ptr,&Head);
- (void)free(ptr); /* free up our original name storage */
- return (tmp); /* return file pointer to the file we opened */
- }
-
- /*
- * mktemp: make temporary filename and return a pointer to it.
- *
- * method: if the string passed has an X in it, insert an 8-digit hexadecimal
- * number based on the current tick count starting at the first X position. If the
- * string passed has no X, insert the 8-digit hexadecimal number starting at
- * the beginning of the string. No action is taken if there is insufficient room
- * to insert the number without extending the passed string.
- */
-
- char *mktemp(ff)
- char *ff;
- {
- char *ptr;
- if ( ( ptr = index(ff, 'x') ) == NULL)
- ptr = ff;
- if (strlen(ptr) >= 8)
- sprintf(ptr, "%lx", (long)TickCount());
- return(ff);
- }
-
- static WDPBRec Mydisk;
- static CInfoPBRec Everything;
- static IsOpen = 0; /* tells me that the file is open */
- static char *name_ptr;
- static char working_dir[255];
- static char holding_file[255];
- static char working_vol[255];
-
- static struct RemoveIt search_result;
-
- /*
- * filedir: search for a file.
- *
- * NOTE: this is not a general file search routine.
- * The search path must have the filename in the form xxxxx.yyyyy. This search
- * code will look for files that match the .yyyyy.
- *
- * filedir(path,value,dest)
- * The name of the matched file (just the filename, no folder/path info) will
- * be returned in dest. An empty dest (first value NULL) indicates no match.
- *
- * value =0: start a new search thread.
- * value =1: use the last search thread, provide the next file match.
- */
- int filedir(path,value,dest)
- char *path;
- int value;
- char *dest;
- {
- WDPBRec MyDisk;
- CInfoPBRec Everything;
- FILE *fp1;
- OSErr e;
-
- char *GetPathname();
- char *ptr,*name_ptr;
- char keep;
- char holding_file[256];
- char working_volume[256];
- char search_string[256];
- char *NLreturn_first();
-
- dest[0] = NULL; /* initialize return value */
-
- /* first, see if this is a new search thread. If so, we must first clean up any
- * outstanding search_result structure.
- */
- if (value == 0) {
- NLdelete_all(&search_result);
- errno = 0;
-
- /* start process of getting directory */
- MyDisk.ioWDProcID = 0L;
- MyDisk.ioWDDirID = 0L;
- MyDisk.ioVRefNum = 0;
- MyDisk.ioWDVRefNum = 0;
- MyDisk.ioNamePtr = (StringPtr)working_volume;
-
- /* establish our path name */
- if (path[0] == NULL)
- strcpy(search_string,":");
- else
- strcpy(search_string,path);
-
- /* see if we have a path with folder info or just a filename */
- if ( (ptr = rindex(search_string, ':')) != NULL) {
- keep = *ptr;
- *ptr = '\0';
- MoveIt(MyDisk.ioNamePtr, search_string);
- *ptr = keep;
- }
- else
- MoveIt(MyDisk.ioNamePtr, search_string);
-
- if ( ( name_ptr = rindex( search_string, '.')) != NULL)
- name_ptr++;
- else
- name_ptr = "";
-
- /*open the working directory */
- if ( (e = PBOpenWD( &MyDisk, FALSE) != noErr) ) {
-
- /* got an error on initial PBOpenWD - see if we were passed a file name
- * instead of directory by trying to open it through the library fopen call
- */
- if ( ( fp1 = fopen(search_string, "r")) == NULL)
- return(-1);
- fclose(fp1); /* close the fopen file */
-
- /* we were able to open the path as a file - return the single file name */
-
- ptr = rindex(search_string, ':');
- if (ptr != NULL) /* found a :, return stuff to right */
- ptr++;
- else
- ptr = search_string; /* no :, return entire path */
- strcpy(dest,ptr); /* copy our single result */
- return(0);
- }
-
- /* original PBOpenWD worked */
-
- Everything.hFileInfo.ioNamePtr = (StringPtr)holding_file;
- Everything.hFileInfo.ioVRefNum = MyDisk.ioVRefNum;
- Everything.hFileInfo.ioFDirIndex = 1;
- Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
- Everything.hFileInfo.ioCompletion = 0;
-
- while( (e = PBGetCatInfo( &Everything, FALSE)) != fnfErr) {
- if ( e == noErr ) {
-
- PtoCstr(holding_file);
- if ( ( ptr = rindex( holding_file, '.')) != NULL) {
- ptr++;
- if ( strncmp(ptr, name_ptr, strlen(name_ptr)) == 0)
- NLadd_name(holding_file, &search_result);
- }
- Everything.hFileInfo.ioFlLgLen = 0;
- Everything.hFileInfo.ioFDirIndex++;
- Everything.hFileInfo.ioDirID = MyDisk.ioWDDirID;
- }
- else
- break; /* got an error other than fnfErr: break */
- }
-
- /* either the PBGetCatInfo finally saw a fnfErr, or some other error occurred. */
-
- PBCloseWD(&MyDisk, FALSE);
- }
-
- /* either we are on a search continuation or have finished the data gathering phase */
-
- if ((ptr = NLreturn_first(&search_result)) != NULL) {
- strcpy(dest,ptr);
- NLdelete_first(&search_result);
- return(0);
- }
- return(-1);
- }
-
- /*
- * Bzero: zero out a buffer
- */
-
- bzero(str, cnt)
- char *str;
- int cnt;
- {
- while(cnt-- != 0)
- *str++ = '\0';
- }
-
- system(str)
- char *str;
- {
- printf("The system subroutine call is not implemented on this version.\n");
- return(-1);
- }
-
- isatty()
- {
- return(1);
- }
-
- /*
- * Access: check the file for access permission. Some of this has to be faked on
- * a Mac, since it does not have access bits.
- *
- * returned value = 0: OK
- * returned value = 1: reject
- */
-
- int
- access(str, perm)
- char *str;
- int perm;
- {
- FILE *fptr;
- CInfoPBRec paramBlock;
- OSErr e;
-
- paramBlock.hFileInfo.ioCompletion = 0;
- paramBlock.hFileInfo.ioVRefNum = 0;
- paramBlock.hFileInfo.ioFDirIndex = 0;
- paramBlock.hFileInfo.ioDirID = 0;
- paramBlock.hFileInfo.ioNamePtr = (StringPtr) CtoPstr(str);
-
- /*
- * Get info on file named in ioNamePtr
- */
-
- e = PBGetCatInfo( ¶mBlock, FALSE);
- PtoCstr(str);
-
- /*
- * if there is an error then find out if the file is present. If so
- * see if user wants to create the file
- */
-
- if ( e != noErr ) {
- if ( ( e == fnfErr) && (perm == 2))
- return(0);
- else
- return(1);
- }
- /*
- * check to see if the file is locked or open. refuse it if it is.
- */
-
- if ( (BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 0)
- || BitTst( ¶mBlock.hFileInfo.ioFlAttrib, 7) ) && perm == 4 )
- return(1);
- else
- return(0);
- }
-
- stat(str, buf)
- char *str;
- struct stat *buf;
- {
- FILE *fptr;
- CInfoPBRec paramBlock;
- OSErr e;
- char *CtoPstr();
-
- paramBlock.hFileInfo.ioCompletion = 0;
- paramBlock.hFileInfo.ioVRefNum = 0;
- paramBlock.hFileInfo.ioFDirIndex = 0;
- paramBlock.hFileInfo.ioDirID = 0;
- paramBlock.hFileInfo.ioNamePtr = (StringPtr) CtoPstr(str);
-
- /*
- * Get info on file named in ioNamePtr
- */
-
- e = PBGetCatInfo( ¶mBlock, FALSE);
- PtoCstr(str);
-
- /*
- * if there is an error then find out if the file is present. If so
- * see if user wants to create the file
- */
-
- if ( e != noErr )
- {
- if ( e == fnfErr )
- {
- return(0);
- }
- else
- {
- return(1);
- }
- }
-
- buf->st_size = paramBlock.hFileInfo.ioFlLgLen + paramBlock.hFileInfo.ioFlRLgLen;
- buf->st_mtime = paramBlock.hFileInfo.ioFlMdDat;
- return(0);
- }
-
- static char *mptr = "MLMXXXXXX";
- char *
- tmpnam(str)
- char *str;
- {
- long tt;
- char *ptr;
- if (str == NULL)
- {
- str = mptr;
- }
- ptr = str;
- ptr += 3;
- tt = TickCount();
- tt &= 0xffffffff;
- sprintf(ptr, "%ld", tt);
- return(str);
- }
-
- /* get environment: only use within KA9Q is to retrieve the local timezone.
- */
-
- char *getenv(str)
- char *str;
- {
- return(tzname[0]);
- }
-
- mac_cleanup()
- {
- struct RemoveIt *tp,*tp1;
-
- /* get our first entry, delete string but leave structure with null pointer */
- tp = &Head;
- if (tp->name_ptr != NULL) {
- (void)unlink(tp->name_ptr);
- (void)free(tp->name_ptr);
- }
- tp1 = tp->next; /* prepare for loop that follows */
- tp->next = NULL; /* initialize pointer to next entry */
- tp->name_ptr = NULL; /* initialize string pointer */
-
- /* now traverse list */
- while(tp1 != NULL) {
- tp = tp1; /* get pointer to current entry */
- tp1 = tp->next; /* prepare pointer to next entry */
- if (tp->name_ptr != NULL) {
- (void)unlink(tp->name_ptr);
- (void)free(tp->name_ptr);
- }
- (void)free(tp);
- }
- unlink(dirbm); /* delete the dirbm.temp file */
- }
- /* ------------------*/
- /* name-list manager */
- /* ------------------*/
-
- /*
- * NLadd_name
- * This routine adds an additional name to a list of structures. A success/failure
- * indication is returned for the caller to examine (0=success, -1=failure).
- */
-
- int NLadd_name(name,first)
- char *name;
- struct RemoveIt *first;
- {
- struct RemoveIt *rptr;
-
- /* get our first entry, traverse list to reach end */
- rptr = first;
- while(rptr->next != NULL ) {
- rptr = rptr->next;
- }
-
- /* allocate space for another list entry */
-
- if ( (rptr->next = (struct RemoveIt *)malloc(sizeof (struct RemoveIt)) ) == NULL) {
- printf("Could not allocate memory for structure RemoveIt\n");
- return(-1);
- }
-
- /* allocate space for the name */
-
- if ( (rptr->name_ptr = malloc(strlen(name)+1) ) == NULL) {
- rptr->next = NULL;
- printf("Could not allocate memory for %s\n", name);
- return(-1);
- }
-
- /* copy the name to the allocated area */
- strcpy(rptr->name_ptr, name);
-
- /* jump to last entry on list and initialize */
- rptr = rptr->next;
- rptr->next = NULL; /* initialize pointer to next entry */
- rptr->name_ptr = NULL; /* initialize string pointer */
-
- return (0); /* return success indication */
- }
-
- /*
- * NLdelete_all
- * This routine deletes all the storage allocated for the list of structures, and leaves
- * the first entry set with a null pointer.
- */
-
- int NLdelete_all(first)
- struct RemoveIt *first;
- {
- struct RemoveIt *rptr;
- struct RemoveIt *next;
-
- /* get our first entry, delete string but leave structure with null pointer */
- rptr = first;
- next = rptr->next;
- if (rptr->name_ptr != NULL)
- (void) free(rptr->name_ptr);
- rptr->next = NULL; /* initialize pointer to next entry */
- rptr->name_ptr = NULL; /* initialize string pointer */
-
- /* now traverse list */
- while(next != NULL) {
- rptr = next;
- next = rptr->next;
- if (rptr->name_ptr != NULL)
- (void) free(rptr->name_ptr);
- (void) free(rptr);
- }
-
- return (0); /* return success indication */
- }
-
- /*
- * NLdelete_first
- * This routine deletes the first entry.
- */
-
- int NLdelete_first(first)
- struct RemoveIt *first;
- {
- struct RemoveIt *rptr;
-
- /* get our first entry, delete string if there is one */
-
- rptr = first->next;
- if (first->name_ptr != NULL)
- (void)free(first->name_ptr); /* delete string */
-
- /* if there are more structures, copy the next one into the first, then
- * delete the storage of the next one */
-
- if (rptr != NULL) {
- first->next = rptr->next; /* copy pointers */
- first->name_ptr = rptr->name_ptr;
- (void)free(rptr); /* delete structure */
- }
-
- return (0); /* return success indication */
- }
-
- /*
- * NLreturn_first
- * This routine returns the string pointer in the first entry.
- */
-
- char *NLreturn_first(first)
- struct RemoveIt *first;
- {
-
- /* get our first entry, return string pointer component */
-
- return(first->name_ptr);
- }
-
- char *strtac(s1,s2)
- register char *s1,*s2;
- {
- register char *result=s1;
- register int n = strlen(s2);
-
- while (*s1++)
- ;
- while(--s1>=result)
- *(s1+n)= *s1;
- s1++;
- while (*s2)
- *s1++=*s2++;
- return result;
- }
-